import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {RechargePage} from "../recharge/recharge";
import {ROUTER} from "../../service/router.service";
import {UserModel} from "../../model/user.model";
import {USER} from "../../service/user.service";
import {PageParam} from "../../api/api.service";
import {BALANCE} from "../../api/balance.service";

/**
 * Generated class for the MyBalancePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-my-balance',
  templateUrl: 'my-balance.html',
})
export class MyBalancePage {
  user:UserModel;
  pageParam:PageParam;
  hasMore:boolean = true;
  billList = [];

  constructor(public BALANCE: BALANCE,public navCtrl: NavController, public navParams: NavParams,private ROUTER: ROUTER,private USER:USER) {
    this.user = this.USER.get();
  }

  gotoRecharge() {
    this.ROUTER.go(ROUTER.RECHARGE);
  }

  ionViewDidLoad() {
    this.user = this.USER.get();
    console.log('ionViewDidLoad MyBalancePage');
    this.doRefresh();
  }

  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.BALANCE.getList(v => {
      this.billList = v.content
      console.log(this.billList);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
    setTimeout(v=>{
      if (refresher) {
        refresher.complete();
      }
    },3000)
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.BALANCE.getList(v => {
      this.hasMore = !v.last;
      this.billList = this.billList.concat(v.content);
      infiniteScroll.complete();
    }, this.pageParam);
    setTimeout(v=>{
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
    },3000)
  }


}
